using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MesDashboardApi.Api.Hubs;

namespace MesDashboardApi.Api.Services
{
    public class DashboardBackgroundService : BackgroundService
    {
        private readonly ILogger<DashboardBackgroundService> _logger;
        private readonly DashboardDataService _dataService;
        private readonly IHubContext<DashboardHub> _hubContext;

        public DashboardBackgroundService(
            ILogger<DashboardBackgroundService> logger,
            DashboardDataService dataService,
            IHubContext<DashboardHub> hubContext)
        {
            _logger = logger;
            _dataService = dataService;
            _hubContext = hubContext;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("Dashboard 后台服务已启动");

            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    // 每秒更新一次时间
                    await _hubContext.Clients.All.SendAsync("ReceiveCurrentTime", _dataService.GetCurrentTime(), stoppingToken);

                    // 每5秒更新一次所有数据
                    if (DateTime.Now.Second % 5 == 0)
                    {
                        // 更新生产计划数据
                        var planData = _dataService.UpdatePlanData();
                        await _hubContext.Clients.All.SendAsync("ReceivePlanData", planData, stoppingToken);

                        // 更新设备运行状态数据
                        var deviceData = _dataService.UpdateDeviceData();
                        await _hubContext.Clients.All.SendAsync("ReceiveDeviceData", deviceData, stoppingToken);

                        // 更新生产概况数据
                        var productionConfig = _dataService.UpdateProductionConfig();
                        await _hubContext.Clients.All.SendAsync("ReceiveProductionConfig", productionConfig, stoppingToken);

                        // 更新生产线状态
                        var stations = _dataService.UpdateProductionStations();
                        await _hubContext.Clients.All.SendAsync("ReceiveProductionStations", stations, stoppingToken);

                        // 更新品质监控数据
                        var qualityData = _dataService.UpdateQualityData();
                        await _hubContext.Clients.All.SendAsync("ReceiveQualityData", qualityData, stoppingToken);

                        // 更新告警信息
                        var alarmList = _dataService.UpdateAlarmList();
                        await _hubContext.Clients.All.SendAsync("ReceiveAlarmList", alarmList, stoppingToken);
                    }

                    await Task.Delay(1000, stoppingToken);
                }
                catch (Exception ex) when (!stoppingToken.IsCancellationRequested)
                {
                    _logger.LogError(ex, "更新仪表盘数据时发生错误");
                    await Task.Delay(5000, stoppingToken);
                }
            }

            _logger.LogInformation("Dashboard 后台服务已停止");
        }
    }
} 